<?php
declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';

$r = \support\Redis::connection();

$stats = [
  'login_ok_at'    => (int)($r->get('gps:login_ok_at') ?? 0),
  'last_connect'   => (int)($r->get('gps:last_connect_at') ?? 0),
  'last_close'     => (int)($r->get('gps:last_close_at') ?? 0),
  'last_error'     => (string)($r->get('gps:last_error') ?? ''),
  'recv_packets'   => (int)($r->get('gps:recv_packets') ?? 0),
  'json_errors'    => (int)($r->get('gps:json_errors') ?? 0),
  'last_packet_at' => (int)($r->get('gps:last_packet_at') ?? 0),
  'saved_rows'     => (int)($r->get('gps:saved_rows') ?? 0),
  'last_saved_at'  => (string)($r->get('gps:last_saved_at') ?? ''),
  'queue_len'      => (int)($r->lLen('gps:json') ?? 0),
];

echo json_encode(['stats' => $stats], JSON_UNESCAPED_UNICODE) . PHP_EOL;

$len = (int)$stats['queue_len'];
if ($len > 0) {
  $items = $r->lRange('gps:json', 0, min(9, $len - 1));
  echo json_encode(['samples' => $items], JSON_UNESCAPED_UNICODE) . PHP_EOL;
} else {
  echo json_encode(['samples' => []], JSON_UNESCAPED_UNICODE) . PHP_EOL;
}